package com.huizu.project.controller;

import com.huizu.common.core.controller.BaseController;
import com.huizu.common.core.domain.entity.SysUser;
import com.huizu.common.core.page.TableDataInfo;
import com.huizu.common.utils.SecurityUtils;
import com.huizu.common.utils.poi.ExcelUtil;
import com.huizu.project.domain.ProUser;
import com.huizu.project.domain.SysUserAuth;
import com.huizu.project.service.ISysUserAuthService;
import com.huizu.project.service.RecordCardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: pjl
 * @Date: 2025/3/3
 * @ClassName: RecordCardController
 * @Description: 成绩模块
 */
@RestController
@RequestMapping("/record/card")
public class RecordCardController extends BaseController {

    @Resource
    private RecordCardService recordCardService;

    @Autowired
    private ISysUserAuthService iSysUserAuthService;

    /**
     * 查询成绩信息
     * @param proUser
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(ProUser proUser) {
        //
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!SecurityUtils.isAdmin(user.getUserId())){
            SysUserAuth sysUserAuthPram = new SysUserAuth();
            sysUserAuthPram.setUserId(user.getUserId());
            List<SysUserAuth> auths = iSysUserAuthService.selectSysUserAuthList(sysUserAuthPram);
            List<String> bjs = auths.stream().filter(auth->auth.getType().equals("bj")).map(SysUserAuth::getBusId).collect(Collectors.toList());
            proUser.setBjIds(bjs);
        }
        startPage();
        List<ProUser> list = recordCardService.list(proUser);
        return getDataTable(list);
    }

    /**
     * 成绩导出
     * @param proUser
     * @param response
     */
    @PostMapping("/export")
    public void export(ProUser proUser, HttpServletResponse response) {
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (!SecurityUtils.isAdmin(user.getUserId())){
            SysUserAuth sysUserAuthPram = new SysUserAuth();
            sysUserAuthPram.setUserId(user.getUserId());
            List<SysUserAuth> auths = iSysUserAuthService.selectSysUserAuthList(sysUserAuthPram);
            List<String> bjs = auths.stream().filter(auth->auth.getType().equals("bj")).map(SysUserAuth::getBusId).collect(Collectors.toList());
            proUser.setBjIds(bjs);
        }
        List<ProUser> list = recordCardService.list(proUser);
        ExcelUtil<ProUser> util = new ExcelUtil<>(ProUser.class);
        util.exportExcel(response,list,"成绩");
    }
}
